import axios from 'axios'

let resLoader = {
    loadObj(filepath) {
        return this.loadText(filepath);
    },

    loadImage(filepath) {
        return new Promise((resolve, reject) => {
            let img = new Image();
            img.src = filepath
            img.onload = function () {
                let w = img.width;
                let h = img.height;
                console.log(`load ${filepath} success!`);
                resolve(img)
            }
            img.onerror = function () {
                console.error(`load ${filepath} error!`)
                reject(filepath)
            }
        });
    },

    loadText(filepath){
        return new Promise((resolve, reject) => {
            axios.get(filepath, {
                // config
                method: 'get',
                responseType: 'text',

                // @ts-ignore
                responseEncoding: 'utf8',
            }).then((resp) => {
                console.log(`load ${filepath} success!`);
                resolve(resp.data);
            }).catch(err => {
                console.error("err:", err);
                reject(filepath);
            })
        })
    }
};

export {resLoader};
